var current_gallery = 0;
var interval_started = 0;
$(function(){
	$( "#coffee_mug" ).load(function() {
		if($(window).innerWidth() > 768)
		{
			$('#section1_wrapper').height($('#section1_bg').height());
		}
		else
		{
			$('#section1_wrapper').css('height','auto');
		}
		var pos = $('.coffee_mug').position();
		$('#coffee_mug_bg').css('left',(pos.left+10)+'px');
	});
});
$(window).resize(function(){
	if($(window).innerWidth() > 768)
	{
		$('#section1_wrapper').height($('#section1_bg').height());
	}
	else
	{
		$('#section1_wrapper').css('height','auto');
	}
	var pos = $('.coffee_mug').position();
	$('#coffee_mug_bg').css('left',(pos.left+10)+'px');
});
function get_entries()
{
	offset += 9;
	page += 1;
	$.ajax({
		url: site_url + '?pagination&contest_id=0&approved=1&limit='+limit+'&offset='+offset,
		success:function(response)
		{
			var entries = jQuery.parseJSON(response);
			//console.log('length'+entries.length);
			var gallery_wrapper = 'gallery_wrapper'+page;
			if(entries.length != 0)
				{
				$('<div id="'+gallery_wrapper+'" style="display:none"></div>').insertAfter($('#gallery_wrapper'+(page-1).toString()));
				}
			var output = '';
			for(var i in entries)
			{
				output += '<div class="col-xs-4 col-md-4 col-sm-3 col-lg-3 gallery cursor_pt gallery_ht" data-attr-name="'+entries[i].name+'" data-attr-pic="'+entries[i].picture+'" data-attr-mmsg="'+entries[i].message+'"><img class="min-width-100 gallery-image" style="" src="upload/'+entries[i].picture+'"></div>';
			}
			$('#gallery_wrapper'+page).html(output);
			if (interval_started == 0)
				{
					setInterval(function(){change_gallery();},5000);
					interval_started = 1;
				}
			if(entries.length != 0)
			{
				get_entries();
			}
		}
	});
}
function change_gallery()
{
//console.log('curr_gallery'+current_gallery);
$("#gallery_wrapper"+(current_gallery.toString())).fadeOut(function(){
if ($('#gallery_wrapper'+((current_gallery+1).toString())).length>0)
	{
	$('#gallery_wrapper'+((current_gallery+1).toString())).fadeIn();
	current_gallery++;
	}
else
	{
	current_gallery = 0;
	$('#gallery_wrapper'+((current_gallery).toString())).fadeIn();
	}});
	
}
$(function(){
	$('.buzz_share_tweet').click();
	get_entries();
	$('#coffee_click_num').html(coffee_click);
	var clicked = 0;
	var sec2 = $('#section2').position();
	
	
	$('#back_top').click(function(){
		$('body').animate({scrollTop:0},1000);
	});
	$('#click_participate').click(function(){
		var pos = $('#coffee_passion').position();
		$('body').animate({scrollTop:pos.top},1000);
	});
	$('.contest_btn_upload').click(function(e){
		e.preventDefault();
		$('#imagefile').click(); 
	});
	$('.gallery-image').click(function(){
		var parent = $(this).parent();
		$('#share_pic').attr('src','upload/'+parent.attr('data-attr-pic'));
		$('#share_msg').html(parent.attr('data-attr-mmsg'));
		$('#share_name').html(parent.attr('data-attr-name').toUpperCase());
	});
	$('#contest_submit').click(function(e){
		e.preventDefault();
		ga_click('submit to upload');
		$('.contest-error').prev('br').remove();
		$('.contest-error').remove();
		$('#contest_form input').each(function(){
			if($(this).attr('name') != 'message' && $(this).val() == '' && $(this).attr('type') != 'hidden')
			{
				$(this).css('background','#FFDFDF');
				if($(this).attr('id') == 'imagefile')
				{
					$('<br><span class="contest-error">Required*</span>').insertAfter($(this));
				}
				else
				{
					$('<span class="contest-error">Required*</span>').insertAfter($(this));
				}
			}
			else
			{
				$(this).css('background','#fff');
				if($(this).attr('id') == 'phone')
				{
					if(!$(this).val().match(/^\d+$/))
					{
						$(this).css('background','#FFDFDF');
						$('<span class="contest-error">Invalid Phone</span>').insertAfter($(this));
					}
					else
					{
						$(this).css('background','#fff');
					}
				}
				if($(this).attr('id') == 'email')
				{
					if(!$(this).val().match(/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum|in|ru|uk|de)\b/))
					{
						$(this).css('background','#FFDFDF');
						$('<span class="contest-error">Invalid Email</span>').insertAfter($(this));
					}
					else
					{
						$(this).css('background','#fff');
					}
				}
			}
		});
		if(!$('#tnc').is(':checked'))
		{
			var next = $('#tnc').next();
			$('<br><span class="contest-error">Please agree to the terms and conditions*</span>').insertAfter(next);
		}
		if($('.contest-error').length == 0)
		{
			$('#contest_form').submit();
		}
	});
	$('#coffee_fill').css('height',( coffee_click / 400000 ) * 100 + '%');
	//var newDate = new Date("July 9, 2014 10:59:59");
	//$('#defaultCountdown').countdown({until: newDate,layout:'<span class="countdown-row countdown-show4"><span class="countdown-section"><div class="disp_inline"><span class="countdown-amount">{dn}</span><span class="countdown-period">{dl}</span></div><div class="disp_inline ctdwn_dots"><span class="countdown_dots">:</span></div></span><span class="countdown-section"><div class="disp_inline"><span class="countdown-amount">{hn}</span><span class="countdown-period">{hl}</span></div><div class="disp_inline ctdwn_dots"><span class="countdown_dots">:</span></div></span><span class="countdown-section"><div class="disp_inline"><span class="countdown-amount">{mn}</span><span class="countdown-period">{ml}</span></div><div class="disp_inline ctdwn_dots"><span class="countdown_dots">:</span></div></span><span class="countdown-section"><span class="countdown-amount">{sn}</span><span class="countdown-period">{sl}</span></span></span>'}); 	
});